﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Rescue_of_the_Dwarvenzombies.Scene.Levels;
using Rescue_of_the_Dwarvenzombies.SceneObjects;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Characters;
using GameLibrary;

namespace Rescue_of_the_Dwarvenzombies.SceneLayers
{
    internal class PlayerLayer : SceneLayer
    {
        private List<Color> playerColors = new List<Color>(4);
        private int nrOfPlayersPC; //Add menu later to choose number of players
        private Game _game;

        public PlayerLayer(Game game)
            : base(game)
        {
            _game = game;
            Depth = Constant.PLAYER_LAYER_DEPTH;
            Child = new WeaponLayer(game);
            LayerName = Constant.Layer.Player;
            nrOfPlayersPC = Constant.NR_OF_PLAYERS;
            InitObjects();
        }

        protected override sealed void InitObjects()
        {
            ILevel level = ((ILevel)_game.Services.GetService(typeof(ILevel)));
       
            playerColors.Add(Color.Coral);
            playerColors.Add(Color.LightGreen);
            playerColors.Add(Color.LightBlue);
            playerColors.Add(Color.Orange);

#if XBOX360
            IInputManager inputManager = ((IInputManager)_game.Services.GetService(typeof(IInputManager)));//.GetActivePlayers();
            bool[] activePlayers = inputManager.GetActivePlayers();
            PlayerIndex playerIndex = 0;
            for (int i = 0; i < activePlayers.Length; i++, playerIndex++)
            {
                if (activePlayers[i])
                    SceneObjects.Add(new Player(playerIndex, _game, this, level.GetPlayerStartingPos(i), playerColors[i]));
            }

#else
            PlayerIndex playerIndex = 0;
            for (int i = 0; i < nrOfPlayersPC; i++, playerIndex++)
            {
                SceneObjects.Add(new Player(playerIndex, _game, this, level.GetPlayerStartingPos(i), playerColors[i]));
            }    
#endif
        }

        public List<ISceneObject> GetSceneObject()
        {
            return SceneObjects;
        }
    }
}